<template>
  <div class="list_container flex both-100">
    <Breadcrumb :title="pageTitle" />
    <div class="content_container">
      <!-- 搜索模块 -->
      <el-form ref="searchMap" :inline="true" :model="searchMap" class="serach_wrapper flex-space-between">
        <div class="search">
          <el-form-item prop="operIp">
            <el-input v-model="searchMap.operIp" placeholder="操作地址" size="small"></el-input>
          </el-form-item>
          <el-form-item prop="title">
            <el-input v-model="searchMap.title" placeholder="系统模块" size="small"></el-input>
          </el-form-item>
          <el-form-item prop="operName">
            <el-input v-model="searchMap.operName" placeholder="操作人员" size="small"></el-input>
          </el-form-item>
          <el-form-item prop="businessType">
            <el-select v-model="searchMap.businessType" placeholder="类型" clearable size="small">
              <el-option v-for="dict in dict.type.sys_oper_type" :key="dict.value" :label="dict.label" :value="dict.value" />
            </el-select>
          </el-form-item>
          <el-form-item prop="status">
            <el-select v-model="searchMap.status" placeholder="状态" clearable size="small">
              <el-option v-for="dict in dict.type.sys_common_status" :key="dict.value" :label="dict.label" :value="dict.value" />
            </el-select>
          </el-form-item>
          <el-form-item prop="operTime" style="width: 240px">
            <el-date-picker v-model="dateRange" style="width: 240px" value-format="yyyy-MM-dd HH:mm:ss" type="daterange" range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期" size="small" :default-time="['00:00:00', '23:59:59']"></el-date-picker>
          </el-form-item>

          <el-form-item class="flex">
            <el-button @click="handleSearch()" class="btn_primary">查询</el-button>
            <el-button @click="handleReset()" class="btn_cancel">重置</el-button>
          </el-form-item>
        </div>
        <div class="edit">
          <el-form-item class="flex">
            <!-- <el-button @click="handleAdd()" class="btn_primary" icon="el-icon-plus"> 新增 </el-button> -->
            <el-button @click="handleDelete" class="btn_primary" icon="el-icon-delete"> 删除 </el-button>
            <el-button @click="handleClean" class="btn_primary" icon="el-icon-delete"> 清空 </el-button>
            <el-button @click="handleExport" class="btn_primary" icon="el-icon-download"> 导出 </el-button>
          </el-form-item>
        </div>
      </el-form>
      <!-- 搜索模块over -->

      <!-- 表格模块 -->
      <div class="table_container">
        <el-table :data="dataList" v-loading="loading" stripe class="per_table" @selection-change="handleSelectionChange" style="width: 100%">
          <el-table-column type="selection" width="50" align="center" />
          <el-table-column label="日志编号" align="center" prop="operId" width="100" />
          <el-table-column label="系统模块" align="center" prop="title" :show-overflow-tooltip="true" />
          <el-table-column label="操作类型" align="center" prop="businessType">
            <template slot-scope="scope">
              <dict-tag :options="dict.type.sys_oper_type" :value="scope.row.businessType" />
            </template>
          </el-table-column>
          <el-table-column label="操作人员" align="center" prop="operName" width="110" :show-overflow-tooltip="true" />
          <el-table-column label="操作地址" align="center" prop="operIp" width="130" :show-overflow-tooltip="true" />
          <el-table-column label="操作状态" align="center" prop="status">
            <template slot-scope="scope">
              <dict-tag :options="dict.type.sys_common_status" :value="scope.row.status" />
            </template>
          </el-table-column>
          <el-table-column label="操作日期" align="center" prop="operTime" width="160">
            <template slot-scope="scope">
              <span>{{ parseTime(scope.row.operTime) }}</span>
            </template>
          </el-table-column>
          <el-table-column label="消耗时间" align="center" prop="costTime" width="160" :show-overflow-tooltip="true" :sort-orders="['descending']">
            <template slot-scope="scope">
              <span>{{ scope.row.costTime }}毫秒</span>
            </template>
          </el-table-column>
          <el-table-column label="操作" width="150" fixed="right">
            <template slot-scope="scope">
              <div class="button-group">
                <!-- <span type="text" class="tag_primary" size="small" @click="handleEdit(scope.row)">编辑</span>
                <span type="text" class="tag_warning" size="small" @click="handleDelete(scope.row)">删除</span> -->
                <span type="text" class="tag_primary" size="small" @click="handleDetail(scope.row)">详情</span>
              </div>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <!-- 表格模块over -->

      <!-- 分页 -->
      <Pagination v-show="total > 0" :total="total" :page.sync="searchMap.pageNum" :limit.sync="searchMap.pageSize" @change="getList"></Pagination>
      <!-- 分页over -->
    </div>

    <!-- 弹窗 新增/编辑 -->
    <OperationForm :formData="formData" :dictMap="dict" :titleIndex="titleIndex" :dialogVisible="dialogVisible" @cancel="cancel"></OperationForm>
    <!-- 弹窗 新增/编辑 over -->

    <!-- 弹窗 详情 -->
    <OperationLogDetail :descData="descData" :dictMap="dict" :dialogVisible="dialogVisibleDetail" @cancel="cancelDetail"></OperationLogDetail>
    <!-- 弹窗 详情 over -->
  </div>
</template>

<script>
import { listOperationLog, delOperationLog, cleanOperationLog } from '@/api/log/operation';
import OperationForm from '@/views/system/log/OperationForm';
import OperationLogDetail from '@/views/system/log/OperationLogDetail';

export default {
  name: 'OperationLog',
  dicts: ['sys_oper_type', 'sys_common_status'],
  data() {
    return {
      // 遮罩层
      loading: false,
      // 日期范围
      dateRange: [],
      // 搜索条件
      searchMap: {
        pageNum: 1,
        pageSize: 10,
        operIp: undefined,
        title: undefined,
        operName: undefined,
        businessType: undefined,
        status: undefined,
      },
      // 数据列表
      dataList: [],
      // 总条数
      total: 0,
      // 日期值
      value1: '',
      // 表单字段数据
      formData: {
        operId: '',
        title: '',
        businessType: '',
        operName: '',
        operLocation: '',
        operIp: '',
        status: '',
        operTime: '',
        costTime: '',
      },
      descData: {},
      // 弹窗-新增，编辑头文字
      titleIndex: 0,
      // 弹窗-新增/编辑-显示隐藏
      dialogVisible: false,
      // 弹窗-详情-显示隐藏
      dialogVisibleDetail: false,
    };
  },
  created() {
    this.getList();
  },
  components: {
    OperationForm,
    OperationLogDetail,
  },
  computed: {
    pageTitle() {
      return '操作日志';
    },
  },
  methods: {
    getList() {
      this.loading = true;
      listOperationLog(this.addDateRange(this.searchMap, this.dateRange)).then(res => {
        if(200 === res.code){
          this.dataList = res.rows;
          this.total = res.total;
        }
        this.loading = false;
      });
    },
    /** 查询按钮操作 */
    handleSearch() {
      this.searchMap.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    handleReset() {
      this.dateRange = [];
      this.resetForm('searchMap');
      this.searchMap.pageNum = 1;
      this.getList()
    },
    /** 多选框选中数据 */
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.operId);
      this.multiple = !selection.length;
    },
    // 新增
    handleAdd() {
      this.titleIndex = 0;
      this.dialogVisible = true;
      this.resetFormData(this.formData);
    },
    // 修改数据
    handleEdit(row) {
      this.changeChar(row);

      // 请求编辑数据后赋值
      this.formData = JSON.parse(JSON.stringify(row));
      this.titleIndex = 1;
      this.dialogVisible = true;
    },
    // 删除数据
    handleDelete(row) {
      const ids = row.operId || this.ids;
      if (this.$utils.isNotEmpty(ids)) {
        this.$confirm('是否确认删除所选数据项？', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning',
        }).then(() => {
          delOperationLog(ids).then(res => {
            if (res.code == 200) {
              this.$message.success('删除成功');
              this.getList();
            }
          });
        });
      } else {
        this.$message.warning('请选择数据进行删除');
      }
    },
    // 清空数据
    handleClean() {
      this.$confirm('是否确认清空所有操作日志数据项？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      })
        .then(function () {
          return cleanOperationLog();
        })
        .then(() => {
          this.getList();
          this.$message.msgSuccess('清空成功');
        });
    },
    // 导出数据
    handleExport() {
      this.download(
        'system/operlog/export',
        {
          ...this.searchMap,
        },
        `操作日志${new Date().getTime()}.xlsx`
      );
    },
    // 查看详情
    handleDetail(row) {
      this.changeChar(row);

      this.dialogVisibleDetail = true;
      // 请求详情数据后赋值
      this.descData = row;
    },
    // 弹窗新增编辑-取消
    cancel() {
      this.dialogVisible = false;
    },
    // 弹窗详情-取消
    cancelDetail() {
      this.dialogVisibleDetail = false;
    },
    // 若依的字典设置值为字符串，必须把数字转为字符串
    changeChar(row) {
      row.businessType = String(row.businessType);
      row.status = String(row.status);
      return row;
    },
  },
};
</script>

<style lang="less" scoped></style>
